package com.project.hqshorttaxi.service.impl;

import com.project.hqshorttaxi.dto.FinishSettingParam;
import com.project.hqshorttaxi.entity.FinishSetting;
import com.project.hqshorttaxi.mapper.FinishSettingMapper;
import com.project.hqshorttaxi.service.FinishSettingService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FinishSettingServiceImpl implements FinishSettingService {

    @Resource
    private FinishSettingMapper finishSettingMapper;

    @Override
    public FinishSetting getById(Integer id) {
        return finishSettingMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(FinishSetting entity) throws Exception {
        finishSettingMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public List<FinishSetting> list(FinishSettingParam param) {
        Example example = new Example(FinishSetting.class);
        Example.Criteria c = example.createCriteria();
        if (param.getBeginTime() != null) {
            c.andGreaterThanOrEqualTo("runDate", param.getBeginTime());
        }
        if (param.getEndTime() != null) {
            c.andLessThanOrEqualTo("runDate", param.getEndTime());
        }
        example.orderBy("runDate").asc();
        List<FinishSetting> list = finishSettingMapper.selectByExample(example);
        return list;
    }
}
